﻿using System.Threading.Tasks;

namespace Sidvall.Net.Mail
{
    public class SmtpClient : ISmtpClient
    {
        #region Public Members

        #region SendAsync

        public async Task SendAsync(MailMessage message)
        {
            System.Net.Mail.Attachment item;

            if (message == null)
                throw new System.ArgumentNullException(nameof(message));

            var recipientAddresses = message.GetRecipientAddressArray();
            var replyToAddresses = message.GetReplyToAddressArray();
            var ccRecipientAddresses = message.GetCCRecipientAddressArray();
            var bccRecipientAddresses = message.GetBccRecipientAddressArray();
            using (System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage())
            {
                if (recipientAddresses != null)
                {
                    foreach (var recipientAddress in recipientAddresses)
                        mail.To.Add(recipientAddress);
                }
                mail.Subject = message.Subject;
                mail.Body = message.Body;
                mail.IsBodyHtml = message.IsBodyHtml;
                if (!string.IsNullOrWhiteSpace(message.FromAddress))
                    mail.From = new System.Net.Mail.MailAddress(message.FromAddress);
                else if (!string.IsNullOrWhiteSpace(SmtpSystemContext.Current.FromAddress))
                    mail.From = new System.Net.Mail.MailAddress(SmtpSystemContext.Current.FromAddress);
                if (!string.IsNullOrWhiteSpace(message.SenderAddress))
                    mail.Sender = new System.Net.Mail.MailAddress(message.SenderAddress);
                if (replyToAddresses != null)
                {
                    foreach (var replyToAddress in replyToAddresses)
                        mail.ReplyToList.Add(replyToAddress);
                }
                if (ccRecipientAddresses != null)
                {
                    foreach (var ccRecipientAddress in ccRecipientAddresses)
                        mail.CC.Add(ccRecipientAddress);
                }
                if (bccRecipientAddresses != null)
                {
                    foreach (var bccRecipientAddress in bccRecipientAddresses)
                        mail.Bcc.Add(bccRecipientAddress);
                }
                if (message.Attachments != null)
                {
                    foreach (var attachment in message.Attachments)
                    {
                        switch (attachment.AttachmentType)
                        {
                            case AttachmentType.File:
                                var stream = await Sidvall.SystemContext.Current.SystemStorageManager.OpenFileAsync(attachment.FileName, Sidvall.IO.FileMode.Open, Sidvall.IO.FileAccess.Read).ConfigureAwait(false);
                                item = new System.Net.Mail.Attachment(stream, attachment.Name);
                                break;
                            case AttachmentType.Stream:
                                item = new System.Net.Mail.Attachment(attachment.ContentStream, attachment.Name);
                                break;
                            default:
                                item = System.Net.Mail.Attachment.CreateAttachmentFromString(attachment.Content, attachment.Name);
                                break;
                        }
                        mail.Attachments.Add(item);
                    }
                }

                // Send the mail
                using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient())
                {
                    if (!string.IsNullOrWhiteSpace(message.ConnectionString))
                        client.Host = message.ConnectionString;
                    else if (!string.IsNullOrWhiteSpace(SmtpSystemContext.Current.Host))
                        client.Host = SmtpSystemContext.Current.Host;
                    client.Send(mail);
                }
            }
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return "SmtpClient - System.Net.Mail.SmtpClient";
        }

        #endregion

        #endregion
    }
}
